
int pin[14]={0,1,2,3,4,5,6,7,8,9,10,11,12,13}; //引脚
int pinmode[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
int pinnum=14;

void setup()
{
  for(int i = 0;i<pinnum;i++){
	  if(pinmode[i]==0){
		pinMode(pin[i],OUTPUT);
		digitalWrite(pin[i],LOW); 
	  } else {
		  pinMode(pin[i],INPUT);
	  }
	
  }
  Serial.begin(115200);//设置串口波特率为115200
  
}

/*********************************************
 * 读取串口指令和数据
 * 要求出口发送过来的数据以c为头，c后为char的命令，d后为int数据，支持-号，e为结束符
 * 如：c123d-12378e
 * 该函数请尽量放在主函数的循环体中
 * *cmd, *data 的值为转换后的数据
 * 返回值:当接收到的数据满足格式要求，且接收完毕，返回0 ，否则返回1
 ********************************************/
int ReadCmd(int *cmd,int *data){
	char c=0;
	static char uart_cin_cmd = 0;
	static int uart_cin_data = 0;
	static char uart_cin_status = 0;  //接受指令的状态 ，和下一个状态
	static int uart_cin_flag = 1;  //
	int uart_cin_fuhao_valid = 1;   //data符号（+-）有效标志位
	while(Serial.available() > 0)
	{
		c = char(Serial.read());
		// 状态机状态切换  -----  解析数据格式 ------
		switch(c){
			case('c'):uart_cin_status = (uart_cin_status==0)?1:0; uart_cin_cmd = 0;continue; //收到指令帧头 将标志设为1，准备接受 命令
			case('d'):{ //收到数据标志 将会读取数据的符号 + -
				if(uart_cin_status==1){
					uart_cin_status = 2;
					uart_cin_data = 0;
				}  else {
					uart_cin_status = 0;
				}
				continue;
			}
			case('e'):{
				if(uart_cin_status==2){
					uart_cin_status = 0;
					*cmd = uart_cin_cmd;
					*data =uart_cin_flag*uart_cin_data ;
					uart_cin_fuhao_valid = 1;
					uart_cin_flag=1;
					return 0;
				} // 接收到数据接收结束标椎   回到初始状态
			}
			default:break;
		}
		switch(uart_cin_status){
			case(1):{
				if(c>='0'&&c<='9')uart_cin_cmd=10*uart_cin_cmd+(c-'0') ;
				else uart_cin_status = 0;
				break;
			}
			case(2):{
				if(c>='0'&&c<='9')uart_cin_data=10*uart_cin_data+(c-'0') ;
				else if( c=='-' && uart_cin_flag ==1 && uart_cin_fuhao_valid==1 ) { uart_cin_flag = -1;uart_cin_fuhao_valid=0;}
				else uart_cin_status = 0;
				break;
			}
			default:{ //复位
				uart_cin_data = 0;
				uart_cin_flag = 1;
				uart_cin_cmd = 0;
				uart_cin_fuhao_valid=1;
			}
		}
	}
	return 1;
}

void help(){
	Serial.println("0.send c255d*e get help" );
	Serial.println("1.Uart send c*d*e,first * is pinnum,second * is operation");
	Serial.println("2.operation=0 -> set output LOW or Get IO level" );
	Serial.println("3.operation=1 -> set output HIGH or Get IO level" );
	Serial.println("4.operation=2 -> reversal output or Get IO level" );
	Serial.println("5.operation=3 -> set pinMode OUTPUT" );
	Serial.println("6.operation=4 -> set pinMode INPUT" );
}
int cmd; 
int data;

//串口设置IO
void cmd_get(){
	if(0 == ReadCmd(&cmd,&data)  ){
	Serial.println();
	Serial.println("-------------------------");
	Serial.print("cmd=");
    Serial.print((unsigned char)cmd); 
	Serial.print(" data=");
    Serial.println(data);
	unsigned char rcmd=(unsigned char)cmd;
	if(rcmd==255){
		help();
		return;
	}
	if(rcmd>=2 && rcmd<=13){

		if(data==3){ //设置引脚为输出
			pinMode(pin[rcmd],OUTPUT);
			pinmode[rcmd] = 0;
			Serial.print("Set D");
			Serial.print(rcmd);
			Serial.println(" OUTPUT mode");
			return ;
		} else if(data==4){ //设置引脚为输入
			pinMode(pin[rcmd],INPUT);
			pinmode[rcmd] = 1;
			Serial.print("Set D");
			Serial.print(rcmd);
			Serial.println(" INPUT mode");
			return ;
		}

		if(data==2) {
			if(pinmode[rcmd]==0){
				int d = digitalRead(pin[rcmd]);
				if(d == HIGH){
					digitalWrite(pin[rcmd],LOW);
					Serial.print("Set D");
					Serial.print(rcmd);
					Serial.println(" = 0");
					return ;
				} 
				if(d == LOW){
					digitalWrite(pin[rcmd],HIGH);
					Serial.print("Set D");
					Serial.print(rcmd);
					Serial.println(" = 1");
					return ;
				} 
			} else {
				int d = digitalRead(pin[rcmd]);
				Serial.print("Get D");
				Serial.print(rcmd);
				Serial.print(" = ");
				Serial.println(d);
			}
		}
		
		if(data==0) {  // 
			if(pinmode[rcmd]==0){
				digitalWrite(pin[rcmd],LOW);
				Serial.print("Set D");
				Serial.print(rcmd);
				Serial.println(" = 0");
			} else {
				int d = digitalRead(pin[rcmd]);
				Serial.print("Get D");
				Serial.print(rcmd);
				Serial.print(" = ");
				Serial.println(d);
			}
			return ;
		}
		if(data==1) {
			if(pinmode[rcmd]==0){
				digitalWrite(pin[rcmd],HIGH);
				Serial.print("Set D");
				Serial.print(rcmd);
				Serial.println(" = 1");
			} else {
				int d = digitalRead(pin[rcmd]);
				Serial.print("Get D");
				Serial.print(rcmd);
				Serial.print(" = ");
				Serial.println(d);
			}
			return ;
		}
	}
  }
}

void loop()
{
	cmd_get();
	
} 


